package com.dhnsoft.web.biz;

import com.dhnsoft.web.entity.Person;
import jakarta.servlet.ServletException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author dhn
 * @version 1.0
 * @date 2023-09-04 11:10
 * @Desc
 */
@Service
@Slf4j
public class UserBizHandler {

    /**
     * 查询指定id的用户
     * */
    public ServerResponse getUser(ServerRequest request){
        log.info("查询【{}】用户信息",request.pathVariable("id"));
        //业务处理
        Person person = new Person(1L,"张三1","zs1@qq.com",15);
        //构造响应
        return ServerResponse.ok().body(person);
    }

    /**
     * 查询多个id的用户
     * 1*/
    public ServerResponse getUsers(ServerRequest request){
        log.info("查询所有用户信息");
        List<Person> list = Arrays.asList(new Person(1L, "张三1", "zs1@qq.com", 15),
                new Person(2L, "张三2", "zs2@qq.com", 16));
        return ServerResponse.ok().body(list);
    }

    /**
     * 添加用户
     * */
    public ServerResponse addUser(ServerRequest request) throws ServletException, IOException {
        Person body = request.body(Person.class);
        log.info("保存用户信息:{}",body);
        return ServerResponse.ok().build();
    }

    /**
     * 修改用户信息
     * */
    public ServerResponse updateUser(ServerRequest request) throws ServletException, IOException {
        Person body = request.body(Person.class);
        log.info("修改用户信息后：{}",body);
        return ServerResponse.ok().build();
    }

    /**
     * 删除用户信息
     * */
    public ServerResponse deleteUser(ServerRequest request){
        log.info("删除用户【{}】信息",request.pathVariable("id"));
        return ServerResponse.ok().build();
    }
}
